package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.Product;
import com.zrrd.yunchmall.product.service.IProductService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/product")
@SuppressWarnings("all")
@Api(tags = "商品服务-商品管理模块")
public class ProductController {
    @Autowired
    private IProductService productService;

    @ApiOperation("退款/关闭订单释放内存")
    @PostMapping("/stock/free")
    public ResponseResult freeStock(@RequestBody List<Map<String,Long>>params){
           productService.freeStock(params);
           return new ResponseResult<>(200,"执行成功!");
    }

    /**
     *
     * @param keyword 关键词
     * @param pageNum 第几页
     * @param pagesize 每页多少数据
     * @param publishStatus 上架状态
     * @param verifyStatus 审核状态
     * @param productSn 商品编码
     * @param productCategoryId 商品类别Id
     * @param brandId 品牌ID
     * @return
     */

    @ApiOperation("查询商品列表")
    @GetMapping("/list")
    public ResponseResult list(@ApiParam("关键词") String keyword,
                               @ApiParam(required = true,defaultValue = "1") @RequestParam int pageNum,
                               @ApiParam(required = true,defaultValue = "5") @RequestParam int pageSize,
                               @ApiParam("上架状态") Integer publishStatus,
                               @ApiParam("审核状态") Integer verifyStatus,
                               @ApiParam("商品编码") String productSn,
                               @ApiParam("商品类别Id") Integer productCategoryId,
                               @ApiParam("品牌ID") Integer brandId){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)){
            queryWrapper.like("name",keyword);
        }
        if (publishStatus!=null){
            queryWrapper.eq("publish_status",publishStatus);
        }
        if (verifyStatus!=null){
            queryWrapper.eq("verify_status",verifyStatus);
        }
        if (!StringUtils.isEmpty(productSn)){
            queryWrapper.eq("producta_sn",productSn);
        }
        if (productCategoryId!=null){
            queryWrapper.eq("product_category_id",productCategoryId);
        }
        if (brandId!=null){
            queryWrapper.eq("brand_id",brandId);
        }
        return new ResponseResult<>(200,"查询成功！",productService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @ApiOperation("删除商品")
    @PostMapping("/update/deleteStatus")
    public ResponseResult updateDeleteStatus(@RequestParam @ApiParam("商品状态") int deleteStatus,
                                             @RequestParam @ApiParam("选中的商品id") String ids){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        //ids=1,2,3
        //update pms_product set delete_status=? where id IN (1,2,3)
        updateWrapper.set("delete_status",deleteStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功！");
    }
    @ApiOperation("新品展示")
    @PostMapping("/update/newStatus")
    public ResponseResult newStatus(@RequestParam @ApiParam("商品新品状态") int newStatus,
                                        @RequestParam @ApiParam("商品id") String ids){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("new_status",newStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
    @ApiOperation("上架状态")
    @PostMapping("/update/publishStatus")
    public ResponseResult publishStatus(@RequestParam @ApiParam("商品推荐状态") int publishStatus,
                                        @RequestParam @ApiParam("商品id") String ids){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        //设置需要改的选项
        updateWrapper.set("publish_status",publishStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
    @ApiOperation("推荐状态")
    @PostMapping("/update/recommendStatus")
    public ResponseResult recommendStatus(@RequestParam @ApiParam("商品推荐状态") int recommendStatus,
                                        @RequestParam @ApiParam("商品id") String ids){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("recommand_status",recommendStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
    @ApiOperation("添加商品信息")
    @PostMapping("/create")
    public ResponseResult createProduct(@RequestBody Product product){
        productService.save(product);

        return new ResponseResult<>(200,"添加成功",product);
    }

    
    @GetMapping("/testSleuth")
    public String testSleuth(){
        return "商品:测试链路追踪";
    }
}
